package AOP;

import jade.lang.acl.ACLMessage;

import java.util.ArrayList;

import Messages.AMessage;
import Messages.AMessage.GlobalNames;

public class Printer 
{
	private static ArrayList<ArrayList<String[]>> regions = new ArrayList<ArrayList<String[]>>();
	private static boolean error = false;
	
	
	static void init()
	{
		ArrayList<String[]> first = new ArrayList<String[]>();
		
		//Office
		String[] area11 = new String[4];
		area11[0] = GlobalNames.getAreaCode(AMessage.worlds[0])+GlobalNames.OFFICE_REGION_AGENT.get();
		area11[1] = "No"; //User in here
		area11[2] = "60"; //preferedTemperature
		area11[3] =  "0"; //Dirty
		
		first.add(area11);
		
		//Heater
		area11 = new String[4];
		area11[0] = GlobalNames.getAreaCode(AMessage.worlds[0])+GlobalNames.FLOOR_HEATER_AGENT.get();
		area11[1] =  "0"; //Heating
		area11[2] = "60"; //setpoint
		area11[3] = "60"; //currentpoint
		
		first.add(area11);
		
		//Sensor
		area11 = new String[2];
		area11[0] = GlobalNames.getAreaCode(AMessage.worlds[0])+GlobalNames.TEMPERATURE_SENSOR_AGENT.get();
		area11[1] =  "60"; //measured
		
		first.add(area11);
		
		//World
		area11 = new String[4];
		area11[0] = AMessage.worlds[0];
		area11[1] = "60"; //actual
		
		first.add(area11);
		
		regions.add(first);
		
		//second region
		ArrayList<String[]> second = new ArrayList<String[]>();
		
		//Office
		String[] area21 = new String[4];
		area21[0] = GlobalNames.getAreaCode(AMessage.worlds[1])+GlobalNames.OFFICE_REGION_AGENT.get();
		area21[1] = "No";
		area21[2] = "60";
		area21[3] =  "0";
		
		second.add(area21);
		
		//Heater
		area21 = new String[4];
		area21[0] = GlobalNames.getAreaCode(AMessage.worlds[1])+GlobalNames.FLOOR_HEATER_AGENT.get();
		area21[1] =  "0";
		area21[2] = "60";
		area21[3] = "60";
		
		second.add(area21);
		
		//Sensor
		area21 = new String[2];
		area21[0] = GlobalNames.getAreaCode(AMessage.worlds[1])+GlobalNames.TEMPERATURE_SENSOR_AGENT.get();
		area21[1] =  "60";
		
		second.add(area21);
		
		//World
		area21 = new String[4];
		area21[0] = AMessage.worlds[1];
		area21[1] = "60";
		
		second.add(area21);
		
		regions.add(second);
		//third region
		ArrayList<String[]> third = new ArrayList<String[]>();
		
		//Office
		String[] area12 = new String[4];
		area12[0] = GlobalNames.getAreaCode(AMessage.worlds[2])+GlobalNames.OFFICE_REGION_AGENT.get();
		area12[1] = "No";
		area12[2] = "60";
		area12[3] =  "0";
		
		third.add(area12);
		
		//Heater
		area12 = new String[4];
		area12[0] = GlobalNames.getAreaCode(AMessage.worlds[2])+GlobalNames.FLOOR_HEATER_AGENT.get();
		area12[1] =  "0";
		area12[2] = "60";
		area12[3] = "60";
		
		third.add(area12);
		
		//Sensor
		area12 = new String[2];
		area12[0] = GlobalNames.getAreaCode(AMessage.worlds[2])+GlobalNames.TEMPERATURE_SENSOR_AGENT.get();
		area12[1] =  "60";
		
		third.add(area12);
		
		//World
		area12 = new String[4];
		area12[0] = AMessage.worlds[2];
		area12[1] = "60";
		
		third.add(area12);
		
		regions.add(third);
		//fourth region
		ArrayList<String[]> fourth = new ArrayList<String[]>();
		
		//Office
		String[] area22 = new String[4];
		area22[0] = GlobalNames.getAreaCode(AMessage.worlds[3])+GlobalNames.OFFICE_REGION_AGENT.get();
		area22[1] = "No";
		area22[2] = "60";
		area22[3] =  "0";
		
		fourth.add(area22);
		
		//Heater
		area22 = new String[4];
		area22[0] = GlobalNames.getAreaCode(AMessage.worlds[3])+GlobalNames.FLOOR_HEATER_AGENT.get();
		area22[1] =  "0";
		area22[2] = "60";
		area22[3] = "60";
		
		fourth.add(area22);
		
		//Sensor
		area22 = new String[2];
		area22[0] = GlobalNames.getAreaCode(AMessage.worlds[3])+GlobalNames.TEMPERATURE_SENSOR_AGENT.get();
		area22[1] =  "60";
		
		fourth.add(area22);
		
		//World
		area22 = new String[4];
		area22[0] = AMessage.worlds[3];
		area22[1] = "60";
		
		fourth.add(area22);
		
		regions.add(fourth);
		
		ArrayList<String[]> misc = new ArrayList<String[]>();

		String[] user = new String[2];
		user[0] = "01"+GlobalNames.USER_AGENT.get(); //name
		user[1] = "(0,0)";							 //location
		
		misc.add(user);
		
		String[] bin = new String[3];
		bin[0] = "01"+GlobalNames.BIN_AGENT.get();	//name
		bin[1] = "0";								//weight
		bin[2] = "0";								//volume
		
		misc.add(bin);
		
		bin = new String[3];
		bin[0] = "02"+GlobalNames.BIN_AGENT.get();	//name
		bin[1] = "0";								//weight
		bin[2] = "0";								//volume
		
		misc.add(bin);
		
		String[] vacuum1 = new String[3];
		vacuum1[0] = "01"+GlobalNames.VACUUM_CLEANER_AGENT.get();	//name
		vacuum1[1] = "IDLE";										//Status
		vacuum1[2] = "100";											//power
		
		misc.add(vacuum1);

		String[] vacuum2 = new String[3];
		vacuum2[0] = "02"+GlobalNames.VACUUM_CLEANER_AGENT.get();	//name
		vacuum2[1] = "IDLE";										//Status
		vacuum2[2] = "100";											//power
		
		misc.add(vacuum2);

		String[] vacuum3 = new String[3];
		vacuum3[0] = "03"+GlobalNames.VACUUM_CLEANER_AGENT.get();	//name
		vacuum3[1] = "IDLE";										//Status
		vacuum3[2] = "100";											//power
		
		misc.add(vacuum3);
		
		regions.add(misc);
	}
	
	public static void Print(String localname, String userIn, int preferedTemperature, int dirty)
	{
		if(!error)
		{
			int number = getRegion(GlobalNames.getAreaCode(localname));
			
			if(((String[])regions.get(number).get(0))[0].equalsIgnoreCase(localname))
			{
				((String[])regions.get(number).get(0))[1] = userIn;
				((String[])regions.get(number).get(0))[2] = Integer.toString(preferedTemperature);
				((String[])regions.get(number).get(0))[3] = Integer.toString(dirty);
			}
			
			print();
		}
	}
	
	public static void Print(String localname, int heating, int setPoint, int currentPoint)
	{
		if(!error)
		{
			int number = getRegion(GlobalNames.getAreaCode(localname));
		
			if(((String[])regions.get(number).get(1))[0].equalsIgnoreCase(localname))
			{
				((String[])regions.get(number).get(1))[1] = Integer.toString(heating);
				((String[])regions.get(number).get(1))[2] = Integer.toString(setPoint);
				((String[])regions.get(number).get(1))[3] = Integer.toString(currentPoint);
			}
			print();
		}
	}
	
	public static void Print(String localname, int temperature)
	{
		if(!error)
		{
			int number = getRegion(GlobalNames.getAreaCode(localname));
		
			if(GlobalNames.decode(localname) == GlobalNames.TEMPERATURE_SENSOR_AGENT)
			{
				if(((String[])regions.get(number).get(2))[0].equalsIgnoreCase(localname))
				{
					((String[])regions.get(number).get(2))[1] = Integer.toString(temperature);
				}
			}
			else
			{
				((String[])regions.get(number).get(3))[1] = Integer.toString(temperature);
			}
			
			print();
		}
	}
	
	public static void Print(String localname, int weight, int volume)
	{
		if(!error)
		{
			int number = getRegion(GlobalNames.getAreaCode(localname));
			
			ArrayList<String[]> misc = regions.get(4);
			
			for(int i = 0; i < misc.size(); i++)
			{
				if(misc.get(i)[0].equalsIgnoreCase(localname))
				{
					misc.get(i)[1] = Integer.toString(weight);
					misc.get(i)[2] = Integer.toString(volume);
				}
			}
			
			print();
		}
	}
	
	public static void Print(String localname, String location) 
	{
		if(!error)
		{
			ArrayList<String[]> misc = regions.get(4);
			
			for(int i = 0; i < misc.size(); i++)
			{
				if(misc.get(i)[0].equalsIgnoreCase(localname))
				{
					misc.get(i)[1] = location;
				}
			}
			
			print();
		}
	}
	
	public static void Print(String localname, String status, int power) 
	{
		if(!error)
		{
			ArrayList<String[]> misc = regions.get(4);
			
			for(int i = 0; i < misc.size(); i++)
			{
				if(misc.get(i)[0].equalsIgnoreCase(localname))
				{
					misc.get(i)[1] = status;
					misc.get(i)[2] = Integer.toString(power);
				}
			}
			
			print();
		}
	}
	
	private static void print()
	{
		String printing = "/=========================================================================================\\\n";
		

		for(int i = 0; i < regions.size(); i++)
		{
			ArrayList<String[]> agents = regions.get(i);
			
			if(i < 4)
			{
				printing = printing + "| " + "Region   ";
			}
			else
			{
				printing = printing + "| " + "Region free";
			}
			
			if(i == 0)
			{
				printing = printing + "11";
			}
			else if(i == 1)
			{
				printing = printing + "21";
			}
			else if(i == 2)
			{
				printing = printing + "12";
			}
			else if(i == 3)
			{
				printing = printing + "22";
			}
			printing = printing + "                                                                             |";
			printing = printing + "\n|=========================================================================================|\n";
			
			for(int j = 0; j < agents.size(); j++)
			{
				
				String[] elementsOfAgent = agents.get(j);
				
				if(GlobalNames.OFFICE_REGION_AGENT == GlobalNames.decode(elementsOfAgent[0]))
				{
					printing = printing + "| " + elementsOfAgent[0];
					
					for(int k = elementsOfAgent[0].length(); k < 25; k++)
					{
						printing = printing + " ";
					}
					
					printing = printing + " # User here: ";
					printing = printing + elementsOfAgent[1];
					for(int k = elementsOfAgent[1].length(); k < 4; k++)
					{
						printing = printing + " ";
					}
					
					printing = printing + " # Pref. temperature: ";
					for(int k = elementsOfAgent[2].length(); k < 4; k++)
					{
						printing = printing + " ";
					}
					printing = printing + elementsOfAgent[2];
					
					printing = printing + " # Dirty pct: ";
					for(int k = elementsOfAgent[3].length(); k < 4; k++)
					{
						printing = printing + " ";
					}
					printing = printing + elementsOfAgent[3] + " |\n";
				}
				else if(GlobalNames.FLOOR_HEATER_AGENT == GlobalNames.decode(elementsOfAgent[0]))
				{
					printing = printing + "| " + elementsOfAgent[0];
					
					for(int k = elementsOfAgent[0].length(); k < 25; k++)
					{
						printing = printing + " ";
					}
					
					printing = printing + " # Heating:   ";
					for(int k = elementsOfAgent[1].length(); k < 4; k++)
					{
						printing = printing + " ";
					}
					printing = printing + elementsOfAgent[1];
					
					printing = printing + " # Setpoint:          ";
					for(int k = elementsOfAgent[2].length(); k < 4; k++)
					{
						printing = printing + " ";
					}
					printing = printing + elementsOfAgent[2];
					
					printing = printing + " # Current:   ";
					for(int k = elementsOfAgent[3].length(); k < 4; k++)
					{
						printing = printing + " ";
					}
					printing = printing + elementsOfAgent[3] + " |\n";
				}
				else if(GlobalNames.USER_AGENT == GlobalNames.decode(elementsOfAgent[0]))
				{
					printing = printing + "| " + elementsOfAgent[0];
					
					for(int k = elementsOfAgent[0].length(); k < 25; k++)
					{
						printing = printing + " ";
					}
					
					printing = printing + " # Location: ";
					for(int k = elementsOfAgent[1].length(); k < 4; k++)
					{
						printing = printing + " ";
					}
					printing = printing + elementsOfAgent[1];
					
					printing = printing + " #                    ";
					for(int k = 0; k < 4; k++)
					{
						printing = printing + " ";
					}
					
					printing = printing + " #            ";
					for(int k = 0; k < 4; k++)
					{
						printing = printing + " ";
					}
					printing = printing +  " |\n";
				}
				else if(GlobalNames.BIN_AGENT == GlobalNames.decode(elementsOfAgent[0]))
				{
					printing = printing + "| " + elementsOfAgent[0];
					
					for(int k = elementsOfAgent[0].length(); k < 25; k++)
					{
						printing = printing + " ";
					}
					
					printing = printing + " # Weight:   ";
					for(int k = elementsOfAgent[1].length(); k < 5; k++)
					{
						printing = printing + " ";
					}
					printing = printing + elementsOfAgent[1];
					
					printing = printing + " # Volume:            ";
					for(int k = elementsOfAgent[2].length(); k < 4; k++)
					{
						printing = printing + " ";
					}
					printing = printing + elementsOfAgent[2];
					
					printing = printing + " #            ";
					for(int k = 0; k < 4; k++)
					{
						printing = printing + " ";
					}
					printing = printing +  " |\n";
				}
				else if(GlobalNames.VACUUM_CLEANER_AGENT == GlobalNames.decode(elementsOfAgent[0]))
				{
					printing = printing + "| " + elementsOfAgent[0];
					
					for(int k = elementsOfAgent[0].length(); k < 25; k++)
					{
						printing = printing + " ";
					}
					
					printing = printing + " # Power:   ";
					for(int k = elementsOfAgent[2].length(); k < 6; k++)
					{
						printing = printing + " ";
					}
					printing = printing + elementsOfAgent[2];
					
					printing = printing + " # Status:   ";
					for(int k = elementsOfAgent[1].length(); k < 13; k++)
					{
						printing = printing + " ";
					}
					printing = printing + elementsOfAgent[1];
					
					printing = printing + " #            ";
					for(int k = 0; k < 4; k++)
					{
						printing = printing + " ";
					}
					printing = printing +  " |\n";
				}
				else if(GlobalNames.TEMPERATURE_SENSOR_AGENT == GlobalNames.decode(elementsOfAgent[0]))
				{
					printing = printing + "| " + elementsOfAgent[0];
					
					for(int k = elementsOfAgent[0].length(); k < 25; k++)
					{
						printing = printing + " ";
					}
					
					printing = printing + " #            ";
					for(int k = 0; k < 4; k++)
					{
						printing = printing + " ";
					}
					
					printing = printing + " # Meas. temperature: ";
					for(int k = elementsOfAgent[1].length(); k < 4; k++)
					{
						printing = printing + " ";
					}
					printing = printing + elementsOfAgent[1];

					printing = printing + " #            ";
					for(int k = 0; k < 4; k++)
					{
						printing = printing + " ";
					}
					printing = printing + " |\n";
				}
				else if(elementsOfAgent[0].substring(2).equalsIgnoreCase(AMessage.worlds[0].substring(2))) //Worlds
				{
					printing = printing + "| " + elementsOfAgent[0];
					
					for(int k = elementsOfAgent[0].length(); k < 25; k++)
					{
						printing = printing + " ";
					}
					
					printing = printing + " #            ";
					for(int k = 0; k < 4; k++)
					{
						printing = printing + " ";
					}
					
					printing = printing + " # Actual temperature:";
					for(int k = elementsOfAgent[1].length(); k < 4; k++)
					{
						printing = printing + " ";
					}					
					printing = printing + elementsOfAgent[1];

					printing = printing + " #            ";
					for(int k = 0; k < 4; k++)
					{
						printing = printing + " ";
					}
					printing = printing + " |\n";
				}
			}
			if(i < regions.size()-1)
				printing = printing + "|=========================================================================================|\n";
			else
				printing = printing + "\\=========================================================================================/\n";
		}
		
		System.out.println(printing);
	}
	
	private static int getRegion(String regionId)
	{
		int re = -1;
		
		if(regionId.equalsIgnoreCase("11"))
		{
			re = 0;
		}
		else if(regionId.equalsIgnoreCase("21"))
		{
			re = 1;
		}
		else if(regionId.equalsIgnoreCase("12"))
		{
			re = 2;
		}
		else if(regionId.equalsIgnoreCase("22"))
		{
			re = 3;
		}
		
		return re;
	}

	public static void error(ACLMessage msg) 
	{
		if(error)
		{
			System.err.println(msg);
		}
		else
		{
			error  = true;
			System.err.println(msg);
		}
		System.exit(1);
	}	
}
